<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="订单号:" prop="order_id.value">
                            <el-input type="textarea" :rows="4" v-model="queryParams.order_id.value" placeholder="请输入订单号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="SKU:" prop="sku.value">
                            <el-input v-model="queryParams.sku.value" placeholder="请输入SKU" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="店铺:" prop="site_id.value">
                            <SelectPage v-model="queryParams.site_id.value" :url="'/admin/site/selectPage'" show_field="name" placeholder="请选择店铺" query_field="name" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="跟踪号:" prop="tracking_number.value">
                            <el-input v-model="queryParams.tracking_number.value" placeholder="请输入跟踪号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="是否刷单:" prop="order_status.value">
                                <el-select v-model="queryParams.order_status.value" placeholder="请选择">
                                    <el-option
                                        v-for="item in order_statusa"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="仓库:" prop="warehouse_id.value">
                            <SelectPage v-model="queryParams.warehouse_id.value" :url="'/admin/warehouse/selectPage'" show_field="title" placeholder="请选择仓库" query_field="title" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="发货类型:" prop="shipping_type.value">
                                <el-select v-model="queryParams.shipping_type.value" placeholder="请选择">
                                    <el-option
                                        v-for="item in shipping_typea"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="下单时间:" prop="createtime.value">
                            <el-date-picker
                                v-model="queryParams.createtime.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :md="6" :sm="12">
                        <el-form-item label="发货时间:" prop="tracking_time.value">
                            <el-date-picker
                                v-model="queryParams.tracking_time.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col> -->
                <!-- <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-plus" size="mini" @click="handleprocessed" v-hasPermi="api.processed">结束发货</el-button>
                </el-col>
                <!-- 
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-plus" size="mini" @click="handleunprocessed" v-hasPermi="api.unprocessed">修改为未处理</el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <!-- <el-form-item label="海外仓" prop=""> -->
                        <el-select
                            v-model="overseas_warehouse"
                            clearable
                            placeholder="请选择海外仓">
                                <el-option
                                    v-for="item in overseas_warehouse_arr"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    <span style="float: left">{{ item.label }}</span>
                                    <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span> -->
                                </el-option>
                        </el-select>
                    <!-- </el-form-item> -->
                </el-col>
                <el-col :span="1.5">
                        <el-select
                            v-model="golden_ant"
                            clearable
                            placeholder="请选择金蚁渠道">
                                <el-option
                                    v-for="item in golden_ant_channel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    <span style="float: left">{{ item.label }}</span>
                                </el-option>
                        </el-select>
                </el-col>
                <!-- <el-col :span="1.5">
                        <el-select
                            v-model="sf_shipping_type"
                            clearable
                            placeholder="请选择顺丰发货类型">
                                <el-option
                                    v-for="item in sf_shipping_typea"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    <span style="float: left">{{ item.label }}</span>
                                </el-option>
                        </el-select>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleModification" v-hasPermi="api.automatic_shipping">自动发货</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleModification2" v-hasPermi="api.automatic_shipping2">强制发货</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>
            <el-card shadow="always">
                <div>
                    <div class="flex justify-between align-center">
                        <div>
                            <p><span style="color:#fd2f2f;">当前美国时区:(-8)：{{ us_time | parseTime('y-m-d h:i:s 星期a') }}</span>
                            <!-- <span style="color:#fd2f2f;">,当前欧洲时区:(+2)：{{ eur_time | parseTime('y-m-d h:i:s 星期a') }}</span> -->
                            </p>
                        </div>
                    </div>
                </div>
            </el-card>
            <!-- 列表 -->
            <el-tabs v-model="activeTab" @tab-click="handleStatus" type="border-card">
                <el-tab-pane v-for="(item, index) in codes" :key="index" :label="item.label" :name="item.value.toString()">
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" align="left">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="info" @click="handleEdit(scope.row)" v-hasPermi="api.edit">手动编辑</el-button>
                            <el-button class="btn-mar" size="mini" type="primary" @click="handleEditlog(scope.row)" v-hasPermi="api.edit">日志</el-button>
                            <!-- <el-button class="btn-mar" size="mini" type="info" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            </el-tab-pane>
            </el-tabs>

            <el-dialog :title="title1" :visible.sync="dialogTableVisible">
                <el-table :data="msgdata">
                    <el-table-column property="msg" label="日志" width=""></el-table-column>
                </el-table>
            </el-dialog>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
       <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpenlog"  :before-close="beforeCloselog" destroy-on-close>
            <EditlogForm ref="editlogForm" :findFormData="findFormDatalog"  :disabled="disabled" @submitForm="submitFormlog" :editFormParams="editFormParams" @editFormCancel="beforeCloselog" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
import EditlogForm from './EditlogFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm,EditlogForm },
    
    data() {
        return {
            shipping_typea: [{
                value: 'SELLER',
                label: 'SELLER'
                }, {
                value: 'TIKTOK',
                label: 'TIKTOK'
            }],
            us_time: 0,
            order_statusa: [{
                value: 'normal',
                label: '正常'
                }, {
                value: 'hidden',
                label: '隐藏'
                }, {
                value: 'brush',
                label: '刷单'
            }],
            overseas_warehouse_arr: [
                {
                    value: '1',
                    label: 'JJS'
                },
                // {
                //     value: '2',
                //     label: '全球无忧美国TK海外仓'
                // },
                {
                    value: '3',
                    label: '金蚁'
                },
                {
                    value: '4',
                    label: '顺丰'
                },

                {
                    value: '5',
                    label: '远邦'
                }
            ],
            golden_ant_channel: [
                {
                    value: '1003',
                    label: 'HWC-TK'
                },
                {
                    value: '2015',
                    label: 'TiK-shop'
                },
                {
                    value: '2011',
                    label: 'HWC-SD'
                },
                {
                    value: '1010',
                    label: 'HWC-artificial'
                },
                {
                    value: '1009',
                    label: '海外仓一6日签'
                }
            ],
            sf_shipping_typea: [
                {
                    value: '1',
                    label: '正常发货'
                },
                {
                    value: '2',
                    label: '超重发货'
                },
            ],
            overseas_warehouse: '3',
            golden_ant: '1003',
            sf_shipping_type: '1',
            dialogTableVisible: false,
            title1: '请等待',
            // table结构
            columns: [
                {
                    visible: true,
                    label: '店铺',
                    prop: 'site_name',
                },
                {
                    visible: true,
                    label: '订单号',
                    prop: 'order_id',
                },
                {
                    visible: true,
                    label: 'SKU详情',
                    prop: 'skus_info',
                },
                {
                    visible: true,
                    label: '订单状态',
                    prop: 'status',
                },
                {
                    visible: true,
                    label: '发货状态',
                    prop: 'tracking_name',
                },
                {
                    visible: true,
                    label: '跟踪号',
                    prop: 'tracking_number',
                },
                {
                    visible: true,
                    label: '仓库',
                    prop: 'warehouse_name',
                },
                {
                    visible: true,
                    label: '发货详情',
                    prop: 'tracking_msg',
                },
                {
                    visible: true,
                    label: '发货类型',
                    prop: 'shipping_type',
                },
                {
                    visible: true,
                    label: '下单时间(美国)',
                    sortable: 'custom',
                    prop: 'createtime',
                },
                {
                    visible: true,
                    label: '发货时间(中国)',
                    sortable: 'custom',
                    prop: 'tracking_time',
                },
                {
                    visible: true,
                    label: '订单类型',
                    prop: 'order_status',
                    component: 'QuickAdminTag',
                    formatter: (prop, row) => {
                        console.log('terowst',row.order_status);
                        if(row.order_status == "normal"){
                            row.order_status = '正常'
                        }
                        if(row.order_status == "hidden"){
                            row.order_status = '隐藏'
                        }
                        if(row.order_status == "brush"){
                            row.order_status = '刷单'
                        }
                        return row.order_status
                    },
                },

            ],
            // 接口地址
            api: {
                index: `/admin/orderlog/index`,
                find: `/admin/orderlog/find`,
                add: `/admin/orderlog/add`,
                edit: `/admin/orderlog/edit`,
                editlog: `/admin/orderlog/editlog`,
                delete: `/admin/orderlog/delete`,
                export: `/admin/orderlog/export`,
                unprocessed: `/admin/orderlog/unprocessed`,
                processed: `/admin/orderlog/processed`,
                automatic_shipping: `/admin/order/automatic_shipping`,
                automatic_shipping2: `/admin/order/automatic_shipping2`,
                status: `/admin/orderlog/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'createtime',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                order_id: { value:undefined, op: '%like%'},
                warehouse_id: { value:undefined, op: '%like%'},
                tracking_number: { value:undefined, op: '%like%'},
                name: { value:undefined, op: '%like%'},
                batch_number: { value:undefined, op: '%like%'},
                fulfillment_type: { value:undefined, op: '%like%'},
                createtime: { value:[], op: 'range'},
                tracking_time: { value:[], op: 'range'},
                tracking_beforehand: { value:[], op: '='},
                sku: { value:undefined, op: '%like%'},
                site_id: { value:[], op: '='},
                order_status: { value:[], op: '='},
                shipping_type: { value:[], op: '='},
            },
            codes: [
                { label: '全部', value: '' },
                { label: '排队发货中', value: '1' },
                { label: '发货异常', value: '2' },
                { label: '回传跟踪号', value: '3' },
                { label: '发货成功', value: '4' },
                { label: '等待海外仓跟踪号', value: '5' },
                { label: '结束发货', value: '6' },
            ],
            activeTab: '',
            msgdata:[],

        }
    },
    created () {
        this.now();
        this.initIndex()
    },
    watch: {
        
    },
    methods: {
        // 北京时间
        now() {
            this.us_time = new Date().getTime() - 16 * 3600 * 1000;
            setInterval(() => {
                this.us_time = new Date().getTime() - 16 * 3600 * 1000;
            }, 1000)
        },
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },

            // 详情
        handleDetail(row) {
            this.handleFindDetail(row)
        },

        async handleFindDetail(row) {
            let response = await this.request.get(this.api.find, { params: {order_id:row.order_id}})
            this.findFormData = this.formatFindData(response.data)
            this.editOpen = true
        },
          //订单状态切换
        handleStatus(tab) {
            this.queryParams.tracking_beforehand.value = tab.name
            this.submitSearch()
        },

        handleprocessed(row) {
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            this.$confirm(`是否确认修改为已处理?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.processeds()
                })
                .catch(() => {})
        },

        async processeds() {
            await this.request.get(this.api.processed, { params: { id:this.ids } })
            this.initIndex()
            this.msgSuccess('操作成功')
        },
        
        handleunprocessed(row) {
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            this.$confirm(`是否确认修改为未处理?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.unprocesseds()
                })
                .catch(() => {})
        },

        async unprocesseds() {
            await this.request.get(this.api.unprocessed, { params: { id:this.ids } })
            this.initIndex()
            this.msgSuccess('操作成功')
        },

        handleModification(row) {
            // console.log('this.ids.length',this.ids.length);
            // if(this.ids.length>10){
            //     return this.msgError('发货一次不超过10条')
            // }
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            if(this.overseas_warehouse == ''){
                return this.msgError('请选择仓库再操作')
            }
            this.$confirm(`是否确认自动发货，过程大概需要一分钟?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.loading = true
                    // this.dialogTableVisible = true;
                    // this.msgdata = [];
                    // this.title1 = '请等待';
                    // test
                    this.automatic_shippings()
                })
                .catch(() => {})
        },

        async automatic_shippings() {
            let response = await this.request.get(this.api.automatic_shipping, { params: { id:this.ids ,warehouse:this.overseas_warehouse,golden_ant:this.golden_ant} })
            // this.msgdata = response.data;
            // this.title1 = '操作成功';
            this.initIndex()
            this.msgSuccess('操作成功，请在发货日志中查看明细')
            this.loading = false
        },

        handleModification2(row) {
            if(this.ids == ''){
                return this.msgError('请勾选再操作')
            }
            if(this.overseas_warehouse == ''){
                return this.msgError('请选择仓库再操作')
            }
            this.$confirm(`是否确认自动发货，过程大概需要一分钟?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.loading = true
                    this.automatic_shippings2()
                })
                .catch(() => {})
        },

        async automatic_shippings2() {
            let response = await this.request.get(this.api.automatic_shipping2, { params: { id:this.ids ,warehouse:this.overseas_warehouse,golden_ant:this.golden_ant} })
            // this.msgdata = response.data;
            // this.title1 = '操作成功';
            this.initIndex()
            this.msgSuccess('操作成功，请在发货日志中查看明细')
            this.loading = false
        },

    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
